
/*
 * Copyright (C) 2008-2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: AbstractSchemaUpdate.java 377 2010-03-11 20:50:18Z archie.cobbs $
 */

package org.dellroad.sidekar.schema.change;

import org.dellroad.sidekar.Sidekar;
import org.dellroad.sidekar.schema.model.SchemaModel;

/**
 * {@link SchemaChange} that adds a new entity to the schema.
 * Newly added entities start with zero fields.
 */
public class AddEntityChange extends EntityChange {

    private final String parentName;
    private final boolean abstractness;

    /**
     * Constructor.
     *
     * @param entityName name of the new entity
     * @param parentName name of the new entity's parent entity, or {@code null} if the new entity has no parent
     * @param abstractness true if entity should be abstract, false if not
     * @throws IllegalArgumentException if either name is not a legal entity name
     */
    public AddEntityChange(String entityName, String parentName, boolean abstractness) {
        super(entityName);
        Sidekar.checkValidEntityName(parentName);
        this.parentName = parentName;
        this.abstractness = abstractness;
    }

    public String getParentName() {
        return this.parentName;
    }

    public boolean getAbstractness() {
        return this.abstractness;
    }

    @Override
    public String getDescription() {
        return "add " + (this.abstractness ? "abstract " : "") + " entity `" + this.getEntityName() + "'"
          + (this.parentName != null ? " to parent `" + this.parentName + "'" : "");
    }

    @Override
    public void checkValidFor(SchemaModel schemaModel) {
        super.checkValidFor(schemaModel);
        this.checkEntityNotExists(schemaModel, this.getEntityName());
        if (this.parentName != null)
            this.checkEntityExists(schemaModel, this.parentName);
    }

    @Override
    public final void visit(SchemaChangeSwitch schemaChangeSwitch) {
        schemaChangeSwitch.caseAddEntityChange(this);
    }
}

